from pml.Eval import frame as Frame, stack as Stack, Runner, io
from pml.PML.tool import CallFunction
import pickle, os

def run(file):
    with open(file, 'rb') as o:
        bytecodes = pickle.load(o)

    variableTable = {}
    variableTable['this'] = variableTable
    const = {}

    stack = Stack.Stack()
    frame = Frame.Frame(file, bytecodes, stack, variableTable, const, stdio=io.Stdio())
    this = Frame.Objects.object.typeobject('<global>')
    for name in variableTable:
        this.methods[name] = variableTable[name]
    runner = Runner.Runner(frame, position=os.path.split(os.path.realpath(file))[0], this=this)
    runner.run(None, runner, position=file)

    if runner.error is not None:
        error = CallFunction.CallFunction(frame, runner.error, runner.error.getMember('__throw__'), runner.errorFrame)
        print(error)
